/* eslint-disable max-len */
import { DesignerHTMLElement, DraggingResolveContext, UseDesignerRules } from "../../../designer-canvas/src/composition/types";
import { useDragulaCommonRule } from "../../../designer-canvas/src/composition/use-dragula-common-rule";
import { ComponentSchema } from "../../../designer-canvas/src/types";
import { getSchemaByType } from "../../../dynamic-resolver/src/schema-resolver";

export function useDesignerRules(schema: ComponentSchema, parentSchema?: ComponentSchema): UseDesignerRules {

    /**
     * 判断是否可以接收拖拽新增的子级控件
     */
    function canAccepts(draggingContext: DraggingResolveContext): boolean {

        const basalRule = useDragulaCommonRule().basalDragulaRuleForContainer(draggingContext);
        if (!basalRule) {
            return false;
        }

        const paneCls = schema.appearance?.class || '';
        const paneClsList = paneCls.split(' ') || [];

        // 若SplitterPane是左侧区域（f-page-content-nav， 表单中一般放置单个列表或树列表），且已有子级节点，则不接收控件
        if (paneClsList.includes('f-page-content-nav')) {
            const firstChild = schema.contents && schema.contents[0];
            return !(firstChild && firstChild.type === 'component');
        }

        return true;
    }

    function onAcceptNewChildElement(element: DesignerHTMLElement, targetPosition: number, componentSchema: ComponentSchema): ComponentSchema {
        const component = schema;
        // 控件本身样式 f-section-in-main
        const componentClass = component.appearance && component.appearance.class || '';
        const componentClassList = componentClass.split(' ') as string[];
        if (component.position === 'main' && componentClassList.includes('f-page-content-main')) {
            if (componentSchema.type === 'tabs') {
                const tabsClassListInPageContentMain = (componentSchema.appearance && componentSchema.appearance.class) ?
                    String(componentSchema.appearance.class).split(' ') : [];
                tabsClassListInPageContentMain.unshift('f-tabs-in-card');
                componentSchema.appearance = { class: tabsClassListInPageContentMain.join(' ') };

                const sectionWrapperElementSchema = getSchemaByType('section') as ComponentSchema;
                sectionWrapperElementSchema.id = `${componentSchema.id}-section`;
                sectionWrapperElementSchema.appearance = { class: "f-section-tabs f-section-in-mainsubcard" };
                sectionWrapperElementSchema.contents = [componentSchema];

                const wrapperElementSchema = getSchemaByType('content-container') as ComponentSchema;
                wrapperElementSchema.id = `${componentSchema.id}-struct-wrapper`;
                wrapperElementSchema.appearance = { class: "f-struct-wrapper" };
                wrapperElementSchema.contents = [sectionWrapperElementSchema];
                return wrapperElementSchema;
            }
            if (componentSchema.type === 'section') {
                const tabsClassListInPageContentMain = (componentSchema.appearance && componentSchema.appearance.class) ?
                    String(componentSchema.appearance.class).split(' ') : [];
                tabsClassListInPageContentMain.unshift('f-section-in-main');
                componentSchema.appearance = { class: tabsClassListInPageContentMain.join(' ') };
                componentSchema.title = String(element.innerText);

                const wrapperElementSchema = getSchemaByType('content-container') as ComponentSchema;
                wrapperElementSchema.id = `${componentSchema.id}-struct-wrapper`;
                wrapperElementSchema.appearance = { class: "f-struct-wrapper" };
                wrapperElementSchema.contents = [componentSchema];
                return wrapperElementSchema;
            }
        }
        return componentSchema;
    }

    return { canAccepts, onAcceptNewChildElement };

}
